﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
 // ERROR: Not supported in C#: OptionDeclaration
using System.Drawing.Drawing2D;
namespace NCS_CS
{
    public partial class Mcurve : System.Windows.Forms.UserControl
	{
		private struct mpoint
		{
			public double x;
			public double y;
		}
		private struct Line
		{
			public double[] xArr;
			public double[] yArr;
			public int n;
			public bool isVisible;
			public int Mode;
			public System.Drawing.Pen  color;
			public int[] yi;
			public int yinum;
		}
		private struct simpleLine
		{
			public mpoint f;
			public mpoint l;
			public int Mode;
			public double k;
			public double b;
		}
        mpoint[] tmpLine;
		bool startTmp;
		public bool showYI=false;
		string xformat;
		string yformat;
		const int cl1 = 0xff00;
        //bool notNeedDelete;
        //int xgn;
			//grid number
        //int ygn;
		public double maxx;
		public double minx;
		public double miny;
			//坐标
		public double maxy;

		double[] lminx;
		double[] lminy;
		double[] lmaxx;
			//线范围
		double[] lmaxy;
		//'draging'
		bool draging;
		int stx,sty,ex,ey,tx,ty;
		//'unZoom use''
		double omaxx;
		double ominx;
		double ominy;
			//坐标
		double omaxy;
		//'
		//Dim x() As Variant, y() As Variant   '信号线坐标数组
		//Dim n() As Long                    '信号线点数数组

			//信号线坐标数组,信号线点数数组
		Line[] l;

		bool zoomed;
			//信号线颜色
		int[] cl;


		//动态生成曲线的临时起点
		//mpoint[] tmpLine;


		int lineCount;
		simpleLine[] sline;

		int slineCount;
		int[] first;
			//显示特殊点
		int[] last;
		bool showPeak;
		bool showPoint;
			//显示特殊点颜色
		Pen incolor;
			//点大小
        //int ps=0;
        //string v="";
		string xdw;

		string ydw;
        //public event myErrEventHandler myErr;
        //public delegate void myErrEventHandler(System.Object Sender, myErrEventArgs e);
        //public event zoomEVEventHandler zoomEV;
        //public delegate void zoomEVEventHandler(System.Object Sender, System.EventArgs e);
        //public event EVmscaleEventHandler EVmscale;
        //public delegate void EVmscaleEventHandler(System.Object Sender, System.EventArgs e);
		//Dim g As Graphics
		System.Drawing.Pen  pen ;
		public void setShowPoint(ref bool b)
		{
			showPoint = b;
			draw();
		}
        public void savelogmsg(string s)
        { 
        }
		public void setshowpeak( bool b)
		{
			showPeak = b;
			draw();
		}


		public void setregion( int f,  int l,  int i)
		{
			first[i] = f;
			last[i] = l;
			//  Call draw
		}
		public void setsimLinecount(ref int a)
		{
			slineCount = a;
			//sline = new simpleLine[slineCount + 1];
            if (sline != null) { Array.Resize(ref sline, a + 1); } else { sline = new simpleLine[a + 1]; }
		}
        public void setLineCount(int a)
        {
            //on error GoTo merr
            startTmp = false;
            int i = 0;
            lineCount = a;
            if (lminx != null) { Array.Resize(ref lminx, a); } else { lminx = new double[a]; }
            if (lminy != null) { Array.Resize(ref lminy, a ); } else { lminy = new double[a ]; }
            if (lmaxx != null) { Array.Resize(ref lmaxx, a ); } else { lmaxx = new double[a ]; }
            if (lmaxy != null) { Array.Resize(ref lmaxy, a ); } else { lmaxy = new double[a ]; }
            //lminx = new double[a + 1];
            //lmaxx = new double[a + 1];
            //lminy = new double[a + 1];
            //lmaxy = new double[a + 1];
            //ReDim x(a), y(a),n(a),
            if (l != null) { Array.Resize(ref l, a ); } else { l = new Line[a ]; }
            //l = new Line[a + 1];
            //cl = new int[a + 1];
            if (cl != null) { Array.Resize(ref cl, a ); } else { cl = new int[a ]; }
            //if (tmpLine != null) { Array.Resize(ref tmpLine, a + 1); } else {tmpLine = new mpoint[a + 1]; }
            if (first != null) { Array.Resize(ref first, a ); } else { first = new int[a ]; }
            if (last != null) { Array.Resize(ref last, a ); } else { last = new int[a ]; }
            if (tmpLine != null) { Array.Resize(ref tmpLine, a); } else { tmpLine = new mpoint[a]; }
            //first = new int[a + 1];
            //last = new int[a + 1];
            for (i = 0; i <= lineCount - 1; i++)
            {
                l[i].color = (System.Drawing.Pen)System.Drawing.Pens.Black.Clone();
                l[i].isVisible = true;
                l[i].Mode = 0;
            }
        }

        //public void setArr(ref double[] vx, ref double[] vy, ref int vn, ref int i)
        //{
        //    l[i].xArr = vx;
        //    l[i].yArr = vy;
        //    l[i].n = vn;
        //    cmdFit.Enabled = true;
        //}
		public void setSline(ref double x1, ref double Y1, ref double X2, ref double Y2, ref int i)
		{
			sline[i].f.x = x1;
			sline[i].f.y = Y1;
			sline[i].l.x = X2;
			sline[i].l.y = Y2;
			sline[i].Mode = 0;
			//two point
		}
		public void setSlinekb(ref double k, ref double b, ref int i)
		{
			sline[i].k = k;
			sline[i].b = b;
			sline[i].Mode = 1;
			//k b
		}
		public bool getLineVisible(ref int i)
		{
			return l[i].isVisible;
		}

		public void setLineVisible(ref int i, ref bool nv)
		{
			if (i < lineCount) {
				l[i].isVisible = nv;
				//  draw
			}
		}
		public void setlineColor( int i,  Color c)
		{
			if (i < lineCount) {
				l[i].color.Color  = c;
				//  draw
			}
		}
		public void setLineMode(ref int i, ref int vmode)
		{
			l[i].Mode = vmode;
			// draw
		}

        public void setArrV( ref double[] vx,  ref double[] vy, int vn, int i)
        {
            l[i].xArr = vx;
            l[i].yArr = vy;
            l[i].n = vn;
            l[i].Mode = 0;
            cmdFit.Enabled = true;
        }

		public void init()
		{
			cmdZoomIn.Enabled = false;
			cmdZoomOut.Enabled = false;
		}

		public void Picture1Cls(ref Graphics g)
		{
			g.Clear(Color.White);
		}
		public void clear()
		{
			//On Error GoTo merr
			if (minx != maxx & miny != maxy) {
				//mScale();

				drawAxis();
			} else {
				savelogmsg("mcurve error scale");
			}
			return;
		}



        public void reset()
        {
            startTmp = false;
            //int i = 0;
            lineCount = 0;
            lminx = null;
            lminy = null;//if (lminy != null) { Array.Resize(ref lminy, a + 1); } else { lminy = new double[a + 1]; }
            lmaxx = null;//if (lmaxx != null) { Array.Resize(ref lmaxx, a + 1); } else { lmaxx = new double[a + 1]; }
            lmaxy = null;//if (lmaxy != null) { Array.Resize(ref lmaxy, a + 1); } else { lmaxy = new double[a + 1]; }
            //lminx = new double[a + 1];
            //lmaxx = new double[a + 1];
            //lminy = new double[a + 1];
            //lmaxy = new double[a + 1];
            //ReDim x(a), y(a),n(a),
            l = null;//if (l != null) { Array.Resize(ref l, a + 1); } else { l = new Line[a + 1]; }
            //l = new Line[a + 1];
            //cl = new int[a + 1];
            cl = null;//if (cl != null) { Array.Resize(ref cl, a + 1); } else { cl = new int[a + 1]; }
            //tmpLine = null;// if (tmpLine != null) { Array.Resize(ref tmpLine, a + 1); } else { tmpLine = new mpoint[a + 1]; }
            first = null;//if (first != null) { Array.Resize(ref first, a + 1); } else { first = new int[a + 1]; }
            last = null;///if (last != null) { Array.Resize(ref last, a + 1); } else { last = new int[a + 1]; }
            //tmpLine = new mpoint[a + 1];
            //first = new int[a + 1];
            //last = new int[a + 1];
            this.Picture1.Refresh(); 
        }
		private void draw()
		{
			Debug.Print("draw");
			clear();
			//drawaxis
			Picture1.Refresh();
		}
		private void mypaint(Graphics g)
		{
			Picture1Cls(ref g);
			int i = 0;
            float m11, m22, dx, dy;
            for (i = 0; i <= lineCount - 1; i++)
            {
                if (l[i].isVisible && l[i].xArr != null && l[i].yArr != null)
                {
                    int jg=1;
                    if (l[i].n > 2000)
                    {
                        jg = l[i].n / 2000;
                    }
                        for (int j = jg; j < l[i].n; j=j+jg)
                        {
                            m11 = (float)(Picture1.Width / (maxx - minx));
                            m22 = (float)(-Picture1.Height / (maxy - miny));
                            dx = (float)((-minx / (maxx - minx)) * Picture1.Width);
                            dy = (float)((miny / (maxy - miny) + 1) * Picture1.Height);
                            if (j >= first[i] && j <= last[i])
                            {
                                g.DrawLine(System.Drawing.Pens.Green  , (float)l[i].xArr[j - jg] * m11 + dx, (float)l[i].yArr[j - jg] * m22 + dy, (float)l[i].xArr[j] * m11 + dx, (float)l[i].yArr[j] * m22 + dy);
                            }
                            else
                            {
                                g.DrawLine(l[i].color, (float)l[i].xArr[j - jg] * m11 + dx, (float)l[i].yArr[j - jg] * m22 + dy, (float)l[i].xArr[j] * m11 + dx, (float)l[i].yArr[j] * m22 + dy);
                            }
                        }
                }
            }
            //if (zoomed)
            //{
            //    //zoomed = false;
            //}
            //else
            //{
               //Picture1Line(ref g, stx, sty, ex, ey);
            
            //}
            if (draging)
            {
                //g.DrawLine(Pens.Red, stx, sty, tx, ty);
                g.DrawRectangle(Pens.Red, toRect(stx, sty,tx,ty));
            }
            else
            {
                //g.DrawLine(Pens.Red, stx, sty, ex, ey);
                if (!zoomed)
                {
                    g.DrawRectangle(Pens.Red, stx, sty, Math.Abs(ex - stx) + 1, Math.Abs(ey - sty) + 1);
                }
            }
            //for (i = 0; i <= slineCount - 1; i++) {
            //    if (sline[i].Mode == 0) {
            //        Picture1Line(ref g, sline[i].f.x, sline[i].f.y, sline[i].l.x, sline[i].l.y);
            //    } else if (sline[i].Mode == 1) {
            //        Picture1Line(ref g, minx, minx * sline[i].k + sline[i].b, maxx, maxx * sline[i].k + sline[i].b);
            //    }
            //}
			//Exit Sub
			//merr:
			//        saveerrmsg()
		}
        public Rectangle toRect(int x1,int y1,int x2,int y2)
        {

            //if (x1 <= x2 && y1 <= y2)
            //{
            //    return new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
            //}
            //else
            int minx, miny;
            if (x1 <= x2)
            {
                minx = x1;
            }
            else 
            { minx = x2; }
            if (y1 <= y2)
            { miny = y1; } 
            else 
            { miny = y2; }
            return new Rectangle(minx,miny, Math.Abs(x2 - x1) + 1, Math.Abs(y2 - y1) + 1);
        }
		public void setScale(ref double vminx, ref double vminy, ref double vmaxx, ref double vmaxy)
		{
			minx = vminx;
			miny = vminy;
			maxx = vmaxx;
			maxy = vmaxy;
			//   Call draw
		}

		public void setGridnumber( int vxgn,  int vygn)
		{
			//on error GoTo merr
			//Dim i As int
			//Dim a As int
			//xgn = vxgn : ygn = vygn
			//a = Label1.Count
			//If a < ygn Then
			//    For i = a To ygn - 1
			//        Label1.Load(i) : Label1(i).Visible = True
			//        'label1(i).Alignment=1'right align
			//        Line1.Load(i) : Line1(i).Visible = True
			//    Next i
			//End If
			//a = Label2.Count
			//If a < xgn Then
			//    For i = a To xgn - 1
			//        Label2.Load(i) : Label2(i).Visible = True
			//        Line2.Load(i) : Line2(i).Visible = True
			//    Next i
			//End If
			//debug.print Label1.count, Label2.count
			//  Stop
			return;
		}
		public void vertline(ref double x1)
		{
			//UPGRADE_ISSUE: PictureBox method Picture1.Line was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//Picture1.Line (x1, minx) - (x1, maxx), cl1
		}
		public void rawLine(ref double x1, ref double Y1, ref double X2, ref double Y2)
		{
			//UPGRADE_ISSUE: PictureBox method Picture1.Line was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//Picture1.Line (x1, Y1) - (X2, Y2)
		}
		public void drawLine(ref Graphics g, ref double[] x11, ref double[] y11,  int countvolt,  Pen c,  int li)
		{
			//On Error GoTo merr
			double r = 0;
			int num = 0;
			double px = 0;
			double py = 0;
			int i = 0;
			if (countvolt < 1)
				return;
			//no data
			num = 0;
			px = x11[num];
			py = y11[num];
			if (showPeak) {
			//  If num >= first(li) And num <= last(li) Then
			//         Picture1.FillColor = incolor
			//  Else
			//         Picture1.FillColor = c
			//  End If
			} else {
			}
			// 0 to countVolt-1
			for (i = 1; i <= countvolt - 1; i++) {
				px = x11[i];
				py = y11[i];
				num = i;
				if (showYI) {
					Picture1Line(ref g, x11[i - 1], y11[i - 1], px, py);
					if (inArr(ref num, ref l[li].yi, ref l[li].yinum)==-1) {
						Picture1Circle(ref g, px, py, r, incolor);
					} else {
						Picture1Circle(ref g, px, py, r, c);
					}
				} else {
					if (showPeak & num >= first[li] & num <= last[li]) {
						Picture1Line(ref g, x11[i - 1], y11[i - 1], px, py);
					} else {
						Picture1Line(ref g, x11[i - 1], y11[i - 1], px, py);
					}
				}
			}

		}
		private void drawLineWithpoint(ref Graphics g, ref double[] x11, ref double[] y11, ref int countvolt, ref int c, ref int li)
		{
			return;
		}
        
		public void myDrawPoint(ref int li, ref int num, ref double px, ref double py, ref int c, ref double r)
		{
			//        On Error GoTo merr
			//        If showYI Then
			//            'UPGRADE_ISSUE: PictureBox method Picture1.Line was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//            Picture1Line(px, py)
			//            If inArr(num, l(li).yi, l(li).yinum) Then
			//                'UPGRADE_ISSUE: PictureBox property Picture1.FillColor was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//                Picture1.FillColor = incolor
			//                'UPGRADE_ISSUE: PictureBox method Picture1.Circle was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//        		Picture1.Circle (px, py), r, incolor
			//            Else
			//                'UPGRADE_ISSUE: PictureBox property Picture1.FillColor was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//                Picture1.FillColor = c
			//                'UPGRADE_ISSUE: PictureBox method Picture1.Circle was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//        		Picture1.Circle (px, py), r, c
			//            End If
			//            'UPGRADE_ISSUE: PictureBox method Picture1.PSet was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//            Picture1.PSet(px, py)
			//        Else
			//            'UPGRADE_ISSUE: PictureBox method Picture1.Line was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//            Picture1.Line(px, py)
			//            If num >= first(li) And num <= last(li) Then
			//                'UPGRADE_ISSUE: PictureBox property Picture1.FillColor was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//                Picture1.FillColor = incolor
			//                'UPGRADE_ISSUE: PictureBox method Picture1.Circle was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//        		Picture1.Circle (px, py), r, incolor

			//            Else
			//                'UPGRADE_ISSUE: PictureBox property Picture1.FillColor was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//                Picture1.FillColor = c
			//                'UPGRADE_ISSUE: PictureBox method Picture1.Circle was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//        		Picture1.Circle (px, py), r, c

			//            End If
			//            'UPGRADE_ISSUE: PictureBox method Picture1.PSet was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//            Picture1.PSet(px, py)
			//        End If
			//        Exit Sub
			//merr:
			//        saveerrmsg()
		}
		public void drawPoint(ref double x, ref double y)
		{
			//UPGRADE_ISSUE: PictureBox method Picture1.PSet was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//Picture1.PSet (x, y), cl1
		}

		public void starttmpline( double x,  double y,  int i)
		{
            Graphics g = Picture1.CreateGraphics();
            if (!startTmp)
            {
                startTmp = true;
                if (x < minx)
                {
                    minx = x;
                }
                else if (x > maxx)
                {
                    maxx = x;
                }
                if (y < miny)
                {
                    miny = y;
                }
                else if (y > maxy)
                {
                    maxy = y;
                }
                drawAxis();
                Picture1Pset(ref g, x, y, l[i].color);
                tmpLine[i].x = x;
                tmpLine[i].y = y;
            }
            else
            {
                //first point but shousuo
                if (x > maxx)
                {
                    if (y > maxy)
                    {
                        //debug.print "shoushuo xy"
                        shouShuoXY();
                    }
                    else if (y < miny)
                    {
                        //debug.print "shoushuo xy _"
                        shouShuoXY_();
                    }
                    else
                    {
                        shouShuoX(ref g);
                    }
                }
                else
                {
                    if (y > maxy)
                    {
                        shouShuoY();
                        //debug.print "shoushuo y"
                    }
                    else if (y < miny)
                    {
                        shouShuoY_();
                        //debug.print "shoushuo y-"
                    }
                }
                Picture1Pset(ref g, x, y,  l[i].color);
                tmpLine[i].x = x;
                tmpLine[i].y = y;
            }
		}

		public void tmplineaddpoint( double x,  double y, int i)
		{
            Graphics g = Picture1.CreateGraphics();
            if (!l[i].isVisible){
                tmpLine[i].x = x ; tmpLine[i].y = y;
            }
        if (x > maxx)
        {
            if( y > maxy)
            {
                //'debug.print "shoushuo xy"
                shouShuoXY();
            }
            else if (y < miny )
            {
                //debug.print "shoushuo xy _"
                shouShuoXY_();
            }
            else
            {
                shouShuoX(ref g);
            }
        }
        else
        {
            if( y > maxy)
            {
                shouShuoY();
                //debug.print "shoushuo y"
            }
            else if(y < miny)
            {
                shouShuoY_();
                //debug.print "shoushuo y-"
            }
        }
        //'UPGRADE_ISSUE: PictureBox method Picture1.PSet was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        //'Picture1.PSet (tmpLine(i).x, tmpLine(i).y), l(i).color
        //'UPGRADE_ISSUE: PictureBox method Picture1.Line was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
        Picture1Line(ref g,tmpLine[i].x,tmpLine[i].y,x, y);//, l(i).color
        tmpLine[i].x = x ; tmpLine[i].y = y;

		}
		//Public Sub drawLineTo(x As single, y As single)
		// Picture1.Line -(x, y), cl
		//End Sub
		private void drawAxis()
		{
			//On Error GoTo merr
            Label1.Text = string.Format("{0:"+yformat +"}", maxy)+ydw;// Convert.ToString(maxy);
            Label2.Text = string.Format("{0:"+yformat+"}", miny)+ydw;// Convert.ToString(miny);
            Label3.Text = string.Format("{0:"+xformat+"}", minx)+xdw;//Convert.ToString(minx);
            Label4.Text = string.Format("{0:"+xformat+"}", maxx)+xdw; //Convert.ToString(maxx);
			//        Exit Sub
			//merr:
			//        saveerrmsg()
		}

        private void Command1_Click(System.Object eventSender, System.EventArgs eventArgs)
        {
            if (stx != ex & sty != ey)
            {
                cmdZoomOut.Enabled = true;
                cmdZoomIn.Enabled = false;
                zoomed = true;
                //notNeedDelete = true;
                ominx = minx;
                ominy = miny;
                omaxx = maxx;
                omaxy = maxy;
                double px1=0, py1=0, px2=0, py2=0;
                toWorld(stx, sty, ref px1,ref py1);
                toWorld(ex, ey, ref px2,ref py2);
                if (px1 > px2)
                {
                    swap(ref px1, ref px2);
                }
                if (py1 > py2)
                {
                    swap(ref py1, ref py2);
                }
                setScale(ref px1, ref py1, ref px2, ref py2);
                draw();
            }
        }

		private void Command2_Click(System.Object eventSender, System.EventArgs eventArgs)
		{
            //notNeedDelete = true;
			cmdZoomOut.Enabled = false;
			cmdZoomIn.Enabled = true;
			setScale(ref ominx, ref ominy, ref omaxx, ref omaxy);
			draw();
		}

		public void fit()
		{
			//On Error GoTo merr
			//notNeedDelete = True
            zoomed = true;
			int i = 0;
			int trueC = 0;
			for (i = 0; i <= lineCount - 1; i++) {
				if (l[i].n > 0 & l[i].isVisible) {
					//lminx[trueC] = longminV(ref l[i].xArr,  l[i].n);
					//lminy[trueC] = longminV(ref l[i].yArr,  l[i].n);
					//lmaxx[trueC] = longmaxV(ref l[i].xArr,  l[i].n);
					//lmaxy[trueC] = longmaxV(ref l[i].yArr,  l[i].n);
                    lminx[trueC] = l[i].xArr[0];
                    lminy[trueC] = l[i].yArr[0];
                    lmaxx[trueC] = l[i].xArr[0];
                    lmaxy[trueC] = l[i].yArr[0];
                    int jg=1;
                    if (l[i].n > 2000)
                    {
                        jg = l[i].n / 2000;
                    }
                    for (int j = jg; j < l[i].n; j=j+jg)
                    {
                        if (lminx[trueC] > l[i].xArr[j])
                        {
                            lminx[trueC] = l[i].xArr[j];
                        }
                        if (lmaxx[trueC] < l[i].xArr[j])
                        {
                            lmaxx[trueC] = l[i].xArr[j];
                        }
                        if (lminy[trueC] > l[i].yArr[j])
                        {
                            lminy[trueC] = l[i].yArr[j];
                        }
                        if (lmaxy[trueC] < l[i].yArr[j])
                        {
                            lmaxy[trueC] = l[i].yArr[j];
                        }

                    }
					trueC = trueC + 1;
				}
			}
			double minx1 = 0;
			double miny1 = 0;
			double maxx1 = 0;
			double maxy1 = 0;
			minx1 = min(ref lminx,  trueC);
			miny1 = min(ref lminy,  trueC);
			maxx1 = max(ref lmaxx,  trueC);
			maxy1 = max(ref lmaxy,  trueC);
			if (minx1 != maxx1) {
				minx = minx1;
				maxx = maxx1;
				minx = minx - (maxx - minx) * 1 / 10;
				maxx = maxx + (maxx - minx) * 1 / 10;
			}
			if (miny1 != maxy1) {
				miny = miny1;
				maxy = maxy1;
				miny = miny - (maxy - miny) * 1 / 10;
				maxy = maxy + (maxy - miny) * 1 / 10;
			}
			draw();
			//        Exit Sub
			//merr:
			//        saveerrmsg()
		}

		private void Command4_Click(System.Object eventSender, System.EventArgs eventArgs)
		{
			fit();
		}

		private void Picture1_MouseDown(System.Object eventSender, System.Windows.Forms.MouseEventArgs eventArgs)
		{
			System.Windows.Forms.MouseButtons  Button= eventArgs.Button;// / 0x100000;
			System.Windows.Forms.Keys   Shift = System.Windows.Forms.Control.ModifierKeys;// / 0x10000;
            int x=eventArgs.X ;
            int y=eventArgs.Y;
			//'debug.print Button
			if (Button ==System.Windows.Forms.MouseButtons.Left ) {
				draging = true;
				if (zoomed) {
					zoomed = false;
				} 
				stx = x;
				sty = y;
				tx = stx;
				ty = sty;
                Picture1.Refresh();
			}
		}

		private void Picture1_MouseMove(System.Object eventSender, System.Windows.Forms.MouseEventArgs eventArgs)
		{
            System.Windows.Forms.MouseButtons Button = eventArgs.Button;// / 0x100000;
            System.Windows.Forms.Keys Shift = System.Windows.Forms.Control.ModifierKeys;// / 0x10000;
            int x = eventArgs.X;
            int y = eventArgs.Y;
			if (draging) {
				//Picture1.Line (stx, sty) - (tx, ty), cl1, B
                tx = x; ty = y;
				//Picture1.Line (stx, sty) - (x, y), cl1, B
                Picture1.Refresh();
                
			}
            //double px = 0, py = 0;
            //toWorld(x, y, ref px, ref py);
            //this.textBox1.Text = px.ToString() + "," + py.ToString();// +"," + ex.ToString() + "," + ey.ToString();
		}

		private void Picture1_MouseUp(System.Object eventSender, System.Windows.Forms.MouseEventArgs eventArgs)
		{
            System.Windows.Forms.MouseButtons Button = eventArgs.Button;// / 0x100000;
            System.Windows.Forms.Keys Shift = System.Windows.Forms.Control.ModifierKeys;// / 0x10000;
            int x=eventArgs.X;
            int y=eventArgs.Y;
			if (draging) {
				cmdZoomIn.Enabled = true;
				draging = false;
				ex = x;
				ey = y;
				if (stx > ex)
					swapint(ref stx, ref ex);
				if (sty > ey)
					swapint(ref sty, ref ey);
                //this.textBox1.Text = stx.ToString() + "," + sty.ToString() + "," + ex.ToString() + "," + ey.ToString();
                Picture1.Refresh();
			}
		}


		private void Picture1_Paint(System.Object eventSender, System.Windows.Forms.PaintEventArgs eventArgs)
		{
			Graphics g = null;
			g = eventArgs.Graphics;
			mypaint(g);
		}

		private void UserControl_Initialize()
		{
			//showPeak = True
            pen=System.Drawing.Pens.Black ;
            Picture1.BackColor = System.Drawing.Color.Black ;
			setGridnumber( 6,  3);
			//picture1.
            //ps = 2;
			xdw = "s";
			ydw = "v";
			xformat = "F0";
			yformat = "F2";
			minx = 0;
			maxx = 5;
			miny = -1;
			maxy = 1;
            zoomed = true;
			//Picture1.Width = UserControl.Width * 0.8
			//Picture1.Height = UserControl.Height * 0.8
			//UserControl.Refresh
			//Call setLineCount(2)
			//mScale();
			drawAxis();
            incolor = (Pen)Pens.Black.Clone();
            
			//Picture1.DrawWidth = 2
			//UPGRADE_ISSUE: PictureBox method Picture1.Cls was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//Picture1.Cls()
			cmdFit.Enabled = false;
			cmdZoomIn.Enabled = false;
			cmdZoomOut.Enabled = false;
		}


		//Handles Command3.Click
		private void Command3_Click(System.Object eventSender, System.EventArgs eventArgs)
		{

		}


		private void UserControl_Terminate()
		{
		}




		public void drawLineAndPoint(ref Graphics g, ref double[] x11, ref double[] y11, ref int countvolt, ref int c, ref int li)
		{
			//		On Error GoTo merr
			//		Dim r As Single
			//		Dim num As Integer
			//		Dim px, py As Single
			//		'  ''debug.print "drawline"
			//		'  ''debug.print countvolt
			//		'UPGRADE_ISSUE: Constant vbUser was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
			//		'UPGRADE_ISSUE: Constant vbPixels was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="55B59875-9A95-4B71-9D6A-7C294BF7139D"'
			//		'UPGRADE_ISSUE: PictureBox method Picture1.ScaleX was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//		r = Picture1.ScaleX(ps, vbPixels, vbUser)
			//		Dim i As Integer
			//		If countvolt < 1 Then Exit Sub 'no data
			//		'drawAxis
			//		'mscale
			//		'Picture1.Cls
			//		'Picture1.Circle (x(0), y(0)), r, c
			//		'  Picture1.FillStyle = 0
			//		'  Picture1.FillStyle = 0
			//		''
			//		'Call myDrawPoint(li, 0, x11(0), Y11(0), c, r)
			//		'  Picture1.ForeColor = c
			//		num = 0
			//		'UPGRADE_WARNING: Couldn't resolve default property of object x11(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			//		px = x11(num)
			//		'UPGRADE_WARNING: Couldn't resolve default property of object y11(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			//		py = y11(num)
			//		If showPeak Then
			//			'  If num >= first(li) And num <= last(li) Then
			//			'         Picture1.FillColor = incolor
			//			'  Else
			//			'         Picture1.FillColor = c
			//			'  End If
			//		Else
			//		End If
			//		'Picture1.Line -(px, py)
			//		'debug.print x(num), Y(num)
			//		'UPGRADE_ISSUE: PictureBox method Picture1.Circle was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//		Picture1.Circle (px, py), r, c
			//		'UPGRADE_ISSUE: PictureBox method Picture1.PSet was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//		Picture1.PSet (px, py), c
			//		''
			//		'Picture1.FillStyle = 0
			//		'debug.print countvolt
			//		If showPeak Then
			//			For i = 1 To countvolt - 1 ' 0 to countVolt-1
			//				'Call myDrawPoint(li, i, CSng(x11(i)), CSng(Y11(i)), c, r)
			//				'UPGRADE_WARNING: Couldn't resolve default property of object x11(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			//				px = x11(i)
			//				'UPGRADE_WARNING: Couldn't resolve default property of object y11(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			//				py = y11(i) : num = i
			//				If num >= first(li) And num <= last(li) Then
			//					'Picture1.FillColor = incolor
			//					'          Picture1.ForeColor = incolor
			//					'UPGRADE_ISSUE: PictureBox method Picture1.Line was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//					Picture1.Line (px, py), incolor
			//					'UPGRADE_ISSUE: PictureBox method Picture1.Circle was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//					Picture1.Circle (px, py), r, incolor
			//				Else
			//					'Picture1.FillColor = c
			//					'          Picture1.ForeColor = c

			//					'UPGRADE_ISSUE: PictureBox method Picture1.Line was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//					Picture1.Line (px, py), c
			//					'UPGRADE_ISSUE: PictureBox method Picture1.Circle was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//					Picture1.Circle (px, py), r, c
			//				End If
			//				'UPGRADE_ISSUE: PictureBox method Picture1.PSet was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//				Picture1.PSet (px, py)
			//			Next i
			//		Else
			//			For i = 1 To countvolt - 1 ' 0 to countVolt-1
			//				'Call myDrawPoint(li, i, CSng(x11(i)), CSng(Y11(i)), c, r)
			//				'UPGRADE_WARNING: Couldn't resolve default property of object x11(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			//				px = x11(i)
			//				'UPGRADE_WARNING: Couldn't resolve default property of object y11(). Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			//				py = y11(i) : num = i
			//				'UPGRADE_ISSUE: PictureBox method Picture1.Line was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//				Picture1.Line (px, py), c
			//				'UPGRADE_ISSUE: PictureBox method Picture1.Circle was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//				Picture1.Circle (px, py), r, c
			//				'UPGRADE_ISSUE: PictureBox method Picture1.PSet was not upgraded. Click for more: 'ms-help://MS.VSExpressCC.v80/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
			//				Picture1.PSet (px, py)
			//			Next i
			//		End If
			//		Exit Sub
			//merr: 
			//		saveerrmsg()
			//		' 'when zoom to  too min region ,it overflow
			//		' 'MsgBox Err.Description, , Str(Err.Number)
			//		' RaiseEvent myErr(Err.Number, Err.Description)
		}

		public void setxyformat( string xf,  string yf)
		{
			xformat = xf;
			yformat = yf;
			drawAxis();
		}
		public void setxyDw( string x,  string y)
		{
			xdw = x;
			ydw = y;
		}

		public void setYiArr(ref int[] yi1, ref int yinum1, ref int lini)
		{
            //l[lini].yi = VB6.CopyArray(ref yi1);
            //l[lini].yinum = yinum1;
		}
		public void setbackcolor(ref int c)
		{
			Picture1.BackColor = System.Drawing.ColorTranslator.FromOle(c);
		}

		public int inArr(ref int a, ref int[] yi, ref int num)
		{
			int functionReturnValue = -1;
			int i = 0;
			for (i = 0; i <= num - 1; i++) {
				if (yi[i] == a) {
					functionReturnValue = i;
					break; // TODO: might not be correct. Was : Exit For
				}
			}
			return functionReturnValue;
		}
		public double longminV(ref double[] x,  int n)
		{
			int i = 0;
			double method1 = 0;
			method1 = x[0];
			for (i = 1; i <= n - 1; i++) {
				if (method1 > x[i])
					method1 = x[i];
			}
			return method1;
		}
		public double longmaxV(ref double[] x,  int n)
		{
			int i = 0;
			double method1 = 0;
			method1 = x[0];
			for (i = 1; i <= n - 1; i++) {
				if (method1 < x[i])
					method1 = x[i];
			}
			return method1;
		}
        public void swapint(ref int x1, ref int X2)
        {
            int t;
            t = x1;
            x1 = X2;
            X2 = t;
        }
		public void swap(ref double x1, ref double X2)
		{
			double t ;
			t = x1;
			x1 = X2;
			X2 = t;
		}
		public double min(ref double[] x,  int n)
		{
			int i = 0;
			double method1 = 0;
			method1 = x[0];
			for (i = 1; i <= n - 1; i++) {
				if (method1 > x[i])
					method1 = x[i];
			}
			return method1;
		}
		public double max(ref double[] x,  int n)
		{
			int i = 0;
			double method1 = 0;
			method1 = x[0];
			for (i = 1; i <= n - 1; i++) {
				if (method1 < x[i])
					method1 = x[i];
			}
			return method1;
		}

		private void Mcurve_Resize(object sender, System.EventArgs e)
		{
			draw();
		}


		public void changeAxis(int index)
		{
			double oldminx = 0;
			double oldmaxx = 0;
			double oldminy = 0;
			double oldmaxy = 0;
			oldminx = minx;
			oldminy = miny;
			oldmaxx = maxx;
			oldmaxy = maxy;
			switch (index) {
				case 1:
					miny = (miny + maxy) / 2f - (maxy - miny);
					//计算精度不够
					maxy = (oldminy + oldmaxy) / 2f + (oldmaxy - oldminy);
					break;
				case 0:
					miny = (miny + maxy) / 2 - (maxy - miny) / 4;
					maxy = (oldminy + oldmaxy) / 2 + (oldmaxy - oldminy) / 4;
					break;
				case 3:
					//x-
					//center  (minx+maxx)/2  r= (maxx-minx)/2
					minx = (minx + maxx) / 2f - (maxx - minx);
					//计算精度不够
					maxx = (oldminx + oldmaxx) / 2f + (oldmaxx - oldminx);
					break;
				//maxx = maxx * 2 'minx=0
				case 2:
					//x+
					minx = (minx + maxx) / 2 - (maxx - minx) / 4;
					maxx = (oldminx + oldmaxx) / 2 + (oldmaxx - oldminx) / 4;
					break;
				//maxx = maxx / 2 'minx=0
			}
			draw();
		}
		private void cmdYBig_Click(System.Object sender, System.EventArgs e)
		{
			changeAxis(0);
		}
		private void cmdYsmall_Click(System.Object sender, System.EventArgs e)
		{
			changeAxis(1);
		}

		private void cmdXbig_Click(System.Object sender, System.EventArgs e)
		{
			changeAxis(2);
		}

		private void cmdXsmall_Click(System.Object sender, System.EventArgs e)
		{
			changeAxis(3);
		}


		private void Picture1_Click(System.Object sender, System.EventArgs e)
		{
		}
		public void Picture1Line(ref Graphics g, double px, double py, double px2, double py2)
		{
            //System.Drawing.Drawing2D.Matrix m = new System.Drawing.Drawing2D.Matrix((float)(Picture1.Width / (maxx - minx)), 0, 0, (float)(-Picture1.Height / (maxy - miny)), (float)((-minx / (maxx - minx)) * Picture1.Width), (float)((miny / (maxy - miny) + 1) * Picture1.Height));
			//System.Drawing.Point[] pt=new System.Drawing.Point[2];
            //pt[0]= new System.Drawing.Point((int)(px*m.Elements[0]+py*m.Elements[1]+m.Elements[4]), (int)(px*m.Elements[2]+py*m.Elements[3]+m.Elements[5]));
            //pt[1] = new System.Drawing.Point((int)(px2 * m.Elements[0] + py2 * m.Elements[1] + m.Elements[4]), (int)(px2 * m.Elements[2] + py2 * m.Elements[3] + m.Elements[5]));
            float m11=(float)(Picture1.Width / (maxx - minx));
            float m22=(float)(-Picture1.Height / (maxy - miny));
            float dx=(float)((-minx / (maxx - minx)) * Picture1.Width);
			float dy= (float)((miny / (maxy - miny) + 1) * Picture1.Height);
			System.Drawing.Point[] pt=new System.Drawing.Point[2];
			pt[0]= new System.Drawing.Point((int)(px*m11+dx), (int)(py*m22+dy));
            pt[1] = new System.Drawing.Point((int)(px2*m11+dx), (int)(py2*m22+dy));
            //m.TransformPoints(pt);
            g.DrawLine(pen, pt[0], pt[1]);
		}
        public void Picture1Pset(ref Graphics g, double px, double py, Pen incolor)
        {
            int x=0, y=0;
            toDisplay(px, py, ref  x, ref  y);
            int r = 1;
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle((int)(x - r), (int)(y - r), (int)(2 * r), (int)(2 * r));
            g.DrawEllipse(incolor, rect);

        }
		public void Picture1Circle(ref Graphics g, double px, double py, double r, Pen incolor)
		{
            int x = 0, y = 0;
            toDisplay(px, py, ref  x, ref  y);
			System.Drawing.Rectangle rect = new System.Drawing.Rectangle((int)(x - r), (int)(y - r), (int)(2 * r), (int)(2 * r));
			g.DrawEllipse(incolor, rect);
		}

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void Picture1_Click_1(object sender, EventArgs e)
        {

        }
        public void setlinecolor(int at,Color c)
        {
            if (at < lineCount)
            {
                l[at].color.Color   = c;
            }
        }
        public void setlinevisible(int at,bool v)
        {
            if (at < lineCount) {
                l[at].isVisible = v;
            }
        }
        private void toWorld( int x,  int y,ref double px, ref double py)
        {
            float m11 = (float)((maxx - minx)/Picture1.Width  );
            float m22 = (float)(-(maxy - miny)/Picture1.Height  );
            float dx =  (float)(minx );
            float dy =  (float)(maxy) ;
            px = x * m11 + dx;
            py = y * m22 + dy;
        }

        private void toDisplay( double px,  double py,ref int x,ref int y)
        {
            float m11 = (float)(Picture1.Width / (maxx - minx));
            float m22 = (float)(-Picture1.Height / (maxy - miny));
            float dx = (float)((-minx / (maxx - minx)) * Picture1.Width);
            float dy = (float)((miny / (maxy - miny) + 1) * Picture1.Height);
            x=(int)(px * m11 + dx);
            y=(int)(py * m22 + dy);
        }
        //public void setregion(int b,int e,int at)
        //{
        //}
        //public void setshowpeak(bool b)
        //{ 
        //}
        //public void starttmpline(double x,double y,int at)
        //{}
        //public void tmplineaddpoint(double x,double y,int at)
        //{}
        void shouShuoY_()
        {
        }
        void shouShuoY(){}
        void shouShuoXY_(){}
        void shouShuoXY(){}
        void shouShuoX(ref Graphics g)
        {
            Image img=(Image)Picture1.Image.Clone();
            this.Picture1Cls(ref g);
            Rectangle rect = new Rectangle(0,0,Picture1.Width/2,Picture1.Height);
            g.DrawImage(img, rect);
            maxx = maxx + (maxx - minx);
            drawAxis();
        }

	}
}
